package com.zhuchaooasystem.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.zhuchaooasystem.R;
import com.zhuchaooasystem.adapter.holder.SingleChoiceHolder;

import java.util.List;


/**
 * @author yinsi
 */
public class SingleChoiceDialog extends AlertDialog {

    private TextView txt_title;
    private EasyRecyclerView recycle;
    private Button btn_ok;
    private Button btn_cancle;
    //----
    private OnViewClickListener listenner;
    //----
    private int cur_position;
    private List<String> departmentList;
    private RecyclerArrayAdapter adapter;

    public SingleChoiceDialog(Context context) {
        super(context);
    }

    protected SingleChoiceDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    public SingleChoiceDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            setContentView(R.layout.dlg_single_choice);
            cur_position = 0;
            //----
            txt_title = (TextView) findViewById(R.id.dialog_single_choice_tv_title);
            recycle = (EasyRecyclerView) findViewById(R.id.dialog_single_choice_recycle);
            btn_ok = (Button) findViewById(R.id.dialog_single_choice_tv_ok);
            btn_cancle = (Button) findViewById(R.id.dialog_single_choice_tv_cancel);
            //----
            recycle.setLayoutManager(new LinearLayoutManager(getContext()));
            recycle.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(getContext()) {
                @Override
                public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                    return new SingleChoiceHolder(parent, SingleChoiceDialog.this);
                }

            });
            btn_ok.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listenner != null) {
                        listenner.onClickOk(SingleChoiceDialog.this, cur_position);
                    } else {
                        dismiss();
                    }
                }
            });
            btn_cancle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (listenner != null) {
                        listenner.onClickCancel(SingleChoiceDialog.this);
                    } else {
                        dismiss();
                    }
                }
            });
        } catch (Exception e) {
        }
    }

    public void setData(List<String> departmentList) {
        this.departmentList = departmentList;
        adapter.clear();
        adapter.addAll(departmentList);
    }

    public void setTextTitle(String title) {
        if (null != txt_title) {
            txt_title.setText(title);
        }
    }

    public interface OnViewClickListener {
        void onClickOk(SingleChoiceDialog dialog, int position);

        void onClickCancel(SingleChoiceDialog dialog);
    }

    public void setOnViewClickListener(OnViewClickListener listener) {
        this.listenner = listener;
    }

    public int getCur_position() {
        return cur_position;
    }

    public void setCur_position(int cur_position) {
        this.cur_position = cur_position;
        adapter.notifyDataSetChanged();
    }
}
